package ${packageName}.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletRequest;
import javax.validation.Valid;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.keta.foundation.dataFilter.DynamicSpecifications;
import ${packageName}.entity.${table.entityName};
import com.keta.foundation.log.Log;
import com.keta.foundation.log.LogUtil;
import ${packageName}.service.${table.entityName}Service;
import com.keta.foundation.util.dwz.AjaxObject;
import com.keta.foundation.util.dwz.Page;


@Controller
@RequestMapping("${requestMapping}")
public class ${table.entityName}Controller {

	private static final Logger logger = LoggerFactory.getLogger(${table.entityName}Controller.class);

	private static final String ${table.entityName?upper_case}_LIST = "${table.instanceName}/list";
	private static final String ${table.entityName?upper_case}_CREATE = "${table.instanceName}/create";
	private static final String ${table.entityName?upper_case}_EDIT = "${table.instanceName}/edit";
	private static final String ${table.entityName?upper_case}_VIEW = "${table.instanceName}/view";
	
	

	@Autowired
	private ${table.entityName}Service ${table.instanceName}Service;
	
	<#if hasDate == true>
	@InitBinder
	public void dataBinder(WebDataBinder dataBinder) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
	}
	</#if>

	/**
	 * ${moduleName}列表入口
	 */
	@RequiresPermissions("${table.entityName}:view")
	@RequestMapping("/list")
	public String list(ServletRequest request, Page page, Model model) {
		Specification<${table.entityName}> specification = DynamicSpecifications.byDataFilter(request, ${table.entityName}.class);
		List<${table.entityName}> ${table.instanceName}List = ${table.instanceName}Service.getAll(specification, page);
		model.addAttribute("${table.instanceName}List", ${table.instanceName}List);
		model.addAttribute("page", page);

		return ${table.entityName?upper_case}_LIST;
	}
	
	/**
	 * 查看${moduleName}
	 */
	@RequiresPermissions("${table.entityName}:view")
	@RequestMapping(value = "/view/{${table.pk.fieldName}}")
	public String view(@PathVariable ${table.pk.javaType} ${table.pk.fieldName}, Model model) {
		${table.entityName} ${table.instanceName} = ${table.instanceName}Service.get(${table.pk.fieldName});
		model.addAttribute("${table.instanceName}", ${table.instanceName});
		return ${table.entityName?upper_case}_VIEW;
	}
	
	
	/**
	 * 添加${moduleName}
	 */
	@RequiresPermissions("${table.entityName}:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String preCreate() {
		return ${table.entityName?upper_case}_CREATE;
	}
	
	
	/**
	 * 添加${moduleName}
	 */
	@ResponseBody
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@Log(message = "添加了${moduleName}:{0}")
	@RequiresPermissions("User:create")
	public String create(@Valid ${table.entityName} ${table.instanceName}) {
		
		${table.instanceName}Service.saveOrUpdate(${table.instanceName});
		LogUtil.putArgs(new Object[] { ${table.instanceName}.toString() });
		return AjaxObject.newOk("${moduleName}添加成功!").toString();
	}
	
	/**
	 * 更新${moduleName}
	 */
	@RequiresPermissions("${table.entityName}:edit")
	@RequestMapping(value = "/edit/{${table.pk.fieldName}}", method = RequestMethod.GET)
	public String preEdit(@PathVariable ${table.pk.javaType} ${table.pk.fieldName}, Model model) {
		${table.entityName} ${table.instanceName} = ${table.instanceName}Service.get(${table.pk.fieldName});
		model.addAttribute("${table.instanceName}", ${table.instanceName});
		return ${table.entityName?upper_case}_EDIT;
	}

	/**
	 * 更新${moduleName}
	 */
	@ResponseBody
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@Log(message = "修改了用户:{0}")
	@RequiresPermissions("${table.entityName}:edit")
	public String edit(@Valid ${table.entityName} ${table.instanceName}) {

		${table.instanceName}Service.saveOrUpdate(${table.instanceName});
		LogUtil.putArgs(new Object[] { ${table.instanceName}.toString() });
		return AjaxObject.newOk("${moduleName}修改成功!").toString();
	}
	

	/*
	 * 删除${moduleName}入口
	 */
	@Log(message = "删除了主键为{0}的${moduleName}")
	@RequiresPermissions("${table.entityName}:delete")
	@RequestMapping("/delete")
	public @ResponseBody String delete(${table.pk.javaType}[] ${table.pk.fieldName}s) {
		String pkStr = "";
		for (${table.pk.javaType} ${table.pk.fieldName} : ${table.pk.fieldName}s) {
			pkStr +=${table.pk.fieldName};
			${table.instanceName}Service.delete(${table.pk.fieldName});
		}
		LogUtil.putArgs(new Object[] { pkStr });
		return AjaxObject.newOk("成功删除了" + ${table.pk.fieldName}s.length + "条${moduleName}！").setCallbackType("").toString();
	}

}
